<template>
  <div class="FollowUser-container">
    <!--关注用户按钮-->
    <van-button v-if="value" class="follow-btn follow-w" round size="small" :loading="loading" @click="onFollow">已关注</van-button>
    <van-button v-else class="follow-btn" type="info" color="#3296fa" round size="small" icon="plus" :loading="loading" @click="onFollow">关注</van-button>
  </div>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {},
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      required: true,
      type: Boolean
    },
    userId: {
      required: true,
      type: [Number, String, Object]
    }
  },
  methods: {
    async onFollow () {
      this.loading = true // 按钮显示加载状态
      try {
        if (this.value) {
          // 已关注，取消关注
          await deleteFollow(this.userId)
          // this.article.is_followed = false // 已关注，按钮显示已关注
        } else {
          // 没有关注，添加关注
          await addFollow(this.userId)
          // this.article.is_followed = true // 未关注，按钮显示未关注
        }
        // this.article.is_followed = !this.article.is_followed // 关注已否，按钮显示已关注/未关注
        // this.$emit('update-is_followed', !this.value)
        this.$emit('input', !this.value)
      } catch (err) {
        let message = '操作失败，请稍后重试！'
        if (err.response && err.response.status === 401) {
          message = '请登录后再关注！'
          this.$router.push('/login')
        }
        this.$toast(message)
      }
      this.loading = false // 按钮不显示加载状态
    }
  }
}
</script>

<style scoped lang="less">
.FollowUser-container{
  .van-button--info{
    padding: 0 20px;
    background-color: #efe614 !important;
    border-color: #efe614 !important;
    color: #000 !important;
  }
  .follow-w{
    padding: 0 23px;
  }
}
</style>
